﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NewsMine.Storage;

namespace NewsMine.Storage
{
    public static class Defaults
    {
        static Defaults()
        {
            queueManager = new QueueManagerOnFileSystem();
            kvStoreManager = new KeyValueStoreManagerOnFileSystem();
        }

        static IQueueManager queueManager = null;
        static IKeyValueStoreManager kvStoreManager = null;

        public static IPersistedQueue JobQueue
        {
            get
            {
                return queueManager.GetDefaultQueue();
            }
        }

        static string uiQueueName = "UIQueue";
        public static IPersistedQueue UIQueue
        {
            get
            {
                return GetQueueByNameWithCreateIfNotExists(uiQueueName);
            }
        }

        static string indexingQueue = "IndexingQueue";
        public static IPersistedQueue IndexingQueue
        {
            get
            {
                return GetQueueByNameWithCreateIfNotExists(indexingQueue);
            }
        }


        static string imageSizeCalculationQueue = "ImageSizeCalculationQueue";
        public static IPersistedQueue ImageSizeCalculationQueue
        {
            get
            {
                return GetQueueByNameWithCreateIfNotExists(imageSizeCalculationQueue);
            }
        }

        static string downloadingJobQueue = "DownloadingJobQueue";
        public static IPersistedQueue DownloadingJobQueue
        {
            get
            {
                return GetQueueByNameWithCreateIfNotExists(downloadingJobQueue);
            }
        }

        static string facebookRealatedDownloadingJobQueue = "FaceBookDownloadingJobQueue";
        public static IPersistedQueue FaceBookDownloadingJobQueue
        {
            get
            {
                return GetQueueByNameWithCreateIfNotExists(facebookRealatedDownloadingJobQueue);
            }
        }

        static string faceBookRetryJobQueue = "FaceBookRetryJobQueue";
        public static IPersistedQueue FaceBookRetryJobQueue
        {
            get
            {
                return GetQueueByNameWithCreateIfNotExists(faceBookRetryJobQueue);
            }
        }


        static string facebookPublishRequestQueue = "FacebookPublishRequestQueue";
        public static IPersistedQueue FacebookPublishRequestQueue
        {
            get
            {
                return GetQueueByNameWithCreateIfNotExists(facebookPublishRequestQueue);
            }
        }

        static string facebookPublishReadyQueue = "FacebookPublishReadyQueue";
        public static IPersistedQueue FacebookPublishReadyQueue
        {
            get
            {
                return GetQueueByNameWithCreateIfNotExists(facebookPublishReadyQueue);
            }
        }

        public static IKeyValueStore DefaultKeyValueStore
        {
            get
            {
                if (kvStoreManager == null)
                {
                    kvStoreManager = new KeyValueStoreManagerOnFileSystem();
                }

                return kvStoreManager.GetDefaultQueue();
            }
        }


        /*
             * setup the system stores
             * =======================
             * 
             * WebSite Master Info Store - which stores the information about the the website domain and Menus + RSS feeds
             * 
             * CrawlingTrackingStore    -   Which stores the information about the when last crawling happened;
             * 
             * HtmlDocumentStore        -   Which Stores the html content against its url. (Need to have maintainance like need to remove old version and archive them)
             * 
             * FeedDocumentStore        -   Which stores the xml feed content against its feed url.
             * 
             * HtmlLinkStore            -   Which Stores the list of HtmlLinks against its url, this can be used to dedup the next visit of the crawler.
             * 
             * FeedItemsStore           -   Stores the FeedItems against url
             * 
             * Setup the Indexes
             * =================
             * 
             * Image index              -   Image search ( referalUrl, Image, altText, imageLink
             * 
             * FeedItem Index           - html 2 feed + feed will be indexed here.
             * 
             */

        static string strWebSiteInfoMasterStore = "WebSiteInfoMasterStore";
        static string strCrawlingTrackingStore = "CrawlingTrackingStore";
        static string strHtmlDocumentStore = "HtmlDocumentStore";
        static string strFeedDocumentStore = "FeedDocumentStore";
        static string strHtmlLinkStore = "HtmlLinkStore";
        static string strHtmlFeedStore = "HtmlFeedStore";
        static string strFeedItemsStore = "FeedItemsStore";
        static string strImageLinkStore = "ImageLinkStore";
        static string strTargetDetailsQueueItemStore = "TargetDetailsQueueItemStore";
        static string strChannelStore = "ChannelStore";
        static string strImageSizeInfoStore = "ImageSizeInfoStore";

        static IKeyValueStore _ChannelStore;
        public static IKeyValueStore ChannelStore
        {
            get
            {
                if (_ChannelStore == null)
                    _ChannelStore = GetKeyValueStoreByNameWithCreateIfNotExists(strChannelStore);

                return _ChannelStore;
            }
        }


        static string strSettingsStore = "SettingsStore";

        static IKeyValueStore _SettingsStore;
        public static IKeyValueStore SettingsStore
        {
            get
            {
                if (_SettingsStore == null)
                    _SettingsStore = GetKeyValueStoreByNameWithCreateIfNotExists(strSettingsStore);

                return _SettingsStore;
            }
        }

        static string strUniqueTitleStore = "UniqueTitleStore";

        static IKeyValueStore _uniqueTitleStore;
        public static IKeyValueStore UniqueTitleStore
        {
            get
            {
                if (_uniqueTitleStore == null)
                    _uniqueTitleStore = GetKeyValueStoreByNameWithCreateIfNotExists(strUniqueTitleStore);

                return _uniqueTitleStore;
            }
        }

        static string str_HtmlCacheStore = "HtmlCacheStore";

        static IKeyValueStore _HtmlCacheStore;
        public static IKeyValueStore HtmlCacheStore
        {
            get
            {
                if (_HtmlCacheStore == null)
                    _HtmlCacheStore = GetKeyValueStoreByNameWithCreateIfNotExists(str_HtmlCacheStore);

                return _HtmlCacheStore;
            }
        }
        static IKeyValueStore _WebSiteInfoMasterStore;
        public static IKeyValueStore WebSiteInfoMasterStore
        {
            get
            {
                if (_WebSiteInfoMasterStore == null)
                    _WebSiteInfoMasterStore = GetKeyValueStoreByNameWithCreateIfNotExists(strWebSiteInfoMasterStore);

                return _WebSiteInfoMasterStore;
            }
        }

        static IKeyValueStore _CrawlingTrackingStore;
        public static IKeyValueStore CrawlingTrackingStore
        {
            get
            {
                if (_CrawlingTrackingStore == null)
                    _CrawlingTrackingStore = GetKeyValueStoreByNameWithCreateIfNotExists(strCrawlingTrackingStore);

                return _CrawlingTrackingStore;
            }
        }

        static IKeyValueStore _ImageSizeInfoStore;
        public static IKeyValueStore ImageSizeInfoStore
        {
            get
            {
                if (_ImageSizeInfoStore == null)
                    _ImageSizeInfoStore = GetKeyValueStoreByNameWithCreateIfNotExists(strImageSizeInfoStore);

                return _ImageSizeInfoStore;
            }
        }




        static IKeyValueStore _HtmlLinkStore;
        public static IKeyValueStore HtmlLinkStore
        {
            get
            {
                if (_HtmlLinkStore == null)
                    _HtmlLinkStore = GetKeyValueStoreByNameWithCreateIfNotExists(strHtmlLinkStore);

                return _HtmlLinkStore;
            }
        }

        static IKeyValueStore _HtmlFeedStore;
        public static IKeyValueStore UniqueHtmlFeedLinkStore
        {
            get
            {
                if (_HtmlFeedStore == null)
                    _HtmlFeedStore = GetKeyValueStoreByNameWithCreateIfNotExists(strHtmlFeedStore);

                return _HtmlFeedStore;
            }
        }


        static IKeyValueStore _TargetDetailsQueueItemStore;
        public static IKeyValueStore TargetDetailsQueueItemStore
        {
            get
            {
                if (_TargetDetailsQueueItemStore == null)
                    _TargetDetailsQueueItemStore = GetKeyValueStoreByNameWithCreateIfNotExists(strTargetDetailsQueueItemStore);

                return _TargetDetailsQueueItemStore;
            }
        }

        #region un necessary properties.

        //static IKeyValueStore _FeedItemsStore;
        //public static IKeyValueStore FeedItemsStore
        //{
        //    get
        //    {
        //        if (_FeedItemsStore == null)
        //            _FeedItemsStore = GetKeyValueStoreByNameWithCreateIfNotExists(strFeedItemsStore);

        //        return _FeedItemsStore;
        //    }
        //}

        #endregion


        public static IKeyValueStore GetKeyValueStoreByNameWithCreateIfNotExists(string keyValueStore)
        {
            IKeyValueStore store = kvStoreManager.GetKeyValueStoreByName(keyValueStore);

            if (store == null)
            {
                kvStoreManager.CreateKeyValueStore(keyValueStore);
                store = kvStoreManager.GetKeyValueStoreByName(keyValueStore);
            }

            return store;
        }

        public static IPersistedQueue GetQueueByNameWithCreateIfNotExists(string queueName)
        {
            IPersistedQueue queue = queueManager.GetQueueByName(queueName);

            if (queue == null)
            {
                queueManager.CreateQueue(queueName);
                queue = queueManager.GetQueueByName(queueName);
            }

            return queue;
        }


     

    }
}
